const urls = [
  "https://img0.baidu.com/it/u=2616823501,3205478532&fm=253&fmt=auto&app=138&f=JPEG?w=509&h=500",
  "https://img0.baidu.com/it/u=3838093562,4126749835&fm=253&fmt=auto&app=138&f=JPEG?w=1144&h=500",
];

const loadImg = (url) =>
  new Promise((resolve, reject) => {
    const img = document.createElement("img");
    img.onload = () => resolve(img);
    img.onerror = () => resolve(null); // Resolve with null if image fails to load
    img.src = url;
  });

(async () => {
  const results = await Promise.allSettled(urls.map(loadImg));
  results.forEach((result) => {
    if (result.status === "fulfilled") {
      if (result.value !== null) {
        document.body.appendChild(result.value);
      } else {
        console.log("One image failed to load.");
      }
    } else {
      console.error(result.reason);
    }
  });
})();

// const urls = [
//     "https://img0.baidu.com/it/u=2616823501,3205478532&fm=253&fmt=auto&app=138&f=JPEG?w=509&h=500",
//     "https://img0.baidu.com/it/u=3838093562,4126749835&fm=253&fmt=auto&app=138&f=JPEG?w=1144&h=500"
// ];

// const loadImg = url => new Promise((resolve, reject) => {
//     const img = document.createElement('img');
//     img.onload = () => resolve(img);
//     img.onerror = () => reject(new Error('图片加载失败'));
//     img.src = url;
// });

// (async () => {
//     try {
//         const imgElements = await Promise.all(urls.map(loadImg));
//         imgElements.forEach(img => document.body.appendChild(img));
//     } catch (error) {
//         console.error(error);
//     }
// })();
